<?php 
    session_start();
    if(!isset($_SESSION['islogined'])||@$_SESSION['idposition']==3)
        header("location:./Login.php");
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <link href="app.css" rel="stylesheet">
    <link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet">

</head>
<body>
    <?php include 'header1.php'; ?>
    <div class="container">
        <ol class="breadcrumb path-nav">
            <li><a href="./Home.php">Home</a></li>
            <li><a href="./Template.php">Template Management</a></li>
            <li class="active">Create Template</li>
        </ol>
    </div>
    <div class="container addstaff-form">
        <h2 class='form-header'>Create New Template</h2>
        <div id="create_tpl_show">
        <?php 
        include "infor_template.php";
        createTemplateShow();
        ?>
        </div>
            <div class="col-md-4 col-md-offset-4 ">         
                <button id="create_tpl" class="btn btn-default btn-lg form-button">Submit</button>
                <button class="btn btn-default btn-lg form-button">Reset</button>
            </div>           
    </div>
    <div class='modal fade' id='myModal' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'></div>
<script src="jquery-3.0.0.min.js"></script>
<script src="bootstrap.min.js"></script>
<script>
 $(function(){
    $(document).on('change','#unittype',function(){
        if($('#unittype').val()=='none'){
            $('.wlu_show').hide();
        }else if($('#unittype').val()=='newtype'){
           $('#unittype').val('none');
           $.post('infor_template.php',{
               createNewType:1
           },function(data){
            $('#myModal').html(data);
            $('#myModal').modal();
           });
        }
        else if($('#unittype').val()=='deltype'){
            $('#unittype').val('none');
           $.post('infor_template.php',{
               createNewType:2
           },function(data){
            $('#myModal').html(data);
            $('#myModal').modal();
           });
        }
        else{
            $('.wlu_show').show();
        }
    })
    $(document).on('click','#delete_type',function(){
        $('.fail-modify').remove();
        $('.success-modify').remove();
        var value=$('#slt_del_type').val();
        if(value!='none'){
            $.post('infor_template.php',{
                deletetype:1,
                typeid:value
            },function(data){
                $('#create_tpl_show').before("<div class='success-modify'>Unit Type Name is deleted successfully</div>");
                $('#create_tpl_show').html(data);
            });
        }
    })
    $(document).on('click','#create_new_type',function(){
        $('.fail-modify').remove();
        $('.success-modify').remove();
        var typename=$('#newtypename').val();
        if(typename==''){
            $('#create_tpl_show').before("<div class='fail-modify'>Unit Type Name is not null</div>");
        }
        else{
            $.post('infor_template.php',{
            insertUnitType:1,
            typename:typename           
            },function(data){
                if(data=='False'){
                    $('#create_tpl_show').before("<div class='fail-modify'>Unit Type Name is Exist</div>");
                }else{
                    $('#create_tpl_show').before("<div class='success-modify'>Unit Type Name is inserted successfully</div>");
                    $('#create_tpl_show').html(data);
                }
            })
        }
        
    })
    $(document).on('click','#create_tpl',function(){
        $('.fail-modify').remove();
        $('.success-modify').remove();
        var tpl_name=$('#tpl_name').val();
        var sup_tpl=$('#sup_tpl').val();
        var unittype=$('#unittype').val();
        var wlu=$('#wlu').val();
        var tpl_ds=$('#tpl_ds').val();
    
        if(tpl_name==''){
            $('.form-header').before("<div class='fail-modify'>Template name can't be empty!</div>");
        }
        else if(unittype!='none'&&wlu==''){
            $('.form-header').before("<div class='fail-modify'>WLU can't be empty!</div>");
        }
        else{
            $.post("infor_template.php",{
                createTemplate:1,
                tpl_name:tpl_name,
                sup_tpl:sup_tpl,
                unittype:unittype,
                wlu:wlu,
                tpl_ds:tpl_ds
            },function(data){
                $('.form-header').before(data);
            });
        }
    })
 })
</script>
</body>
</html>